package com.yvan.onekeylock;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentSender;
import android.util.Log;

import com.huawei.android.quickaction.ActionIcon;
import com.huawei.android.quickaction.QuickAction;
import com.huawei.android.quickaction.QuickActionService;

import java.util.ArrayList;
import java.util.List;

public class MyQuickActionService extends QuickActionService {

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("MyQuickActionService", "onCreate");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("MyQuickActionService", "onDestroy");
    }

    @Override
    public List<QuickAction> onGetQuickActions(ComponentName componentName) {
        ArrayList<ComponentName> components = new ArrayList<>();
        ComponentName commandComp = new ComponentName(this, CommandActivity.class);
        components.add(commandComp);
        commandComp = new ComponentName(this, CommandActivity.class);
        components.add(commandComp);

        // The list of Quick Action items.
        ArrayList<QuickAction> actions = new ArrayList<>();
        for (int i = 0; i < components.size(); i++) {
            Intent intent = new Intent();
            CommandItem commandItem = CommandItem.byId(i);

            // You can use explicit or implicit intent as you like.
            // Demo uses explicit intent.
            intent.setComponent(components.get(i));

            //传送指令代号
            intent.putExtra(CommandItem.COMMAND, commandItem.getCommandCode());
            // NOTE: if you start different activities, then you should use the same requestCode.
            // Otherwise use different requestCode.
            // Demo starts two different activities, so use the same requestCode, here is 0.
            PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
            IntentSender sender = pi.getIntentSender();
            actions.add(new QuickAction(
                    // The title of this action.
                    commandItem.getName(),
                   /* // The summary of this action.
                    // We support summary here.
                    commandItem.getName(),*/
                    // The icon to represent this action.
                    ActionIcon.createWithResource(this, commandItem.getIcon()),
                    // The name of the component to be launched if this action is chosen.
                    components.get(i),
                    // The target action to perform with it.
                    sender));
        }

        return actions;
    }

}
